package com.beiding.authorization.wechat;


import com.beiding.exception.AuthorityException;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/*
    微信登录
 */
@Component
public class WeChatAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        try {
            //TODO 创建第三方捆绑账户
            List<GrantedAuthority> ls = new ArrayList<>();
            ls.add(new SimpleGrantedAuthority("user"));
            if (authentication instanceof WeChatAuthentication) {
                ((WeChatAuthentication) authentication).setAuthorities(ls);
            }
            authentication.setAuthenticated(true);
        }
        catch (Exception e)
        {
            throw new AuthorityException(e.getMessage());
        }

        return authentication;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication == WeChatAuthentication.class;
    }

}
